
#include "include/gtest.h"
#include "../Tool/include/MemoryPool.h"
#include "../Tool/include/ILogger.h"
#include "../Tool/include/Timer.h"

using namespace Tool;

TEST(MemoryPool, MemoryPoolTest)
{
    class TestObject {
    private:
        int value_;
    public:
        TestObject() : value_(0) {}
        TestObject(int value) : value_(value) {}
        ~TestObject() {}
        int getValue() const { return value_; }
    };

    auto MemoryPoolTest = [](int count)
    {
        Timer timer;
        MemoryPool<TestObject> pool;
        std::vector<TestObject*> objects;
        objects.reserve(count);
        for (size_t i = 0; i < count; ++i)
        {
            TestObject* obj = pool.allocate();
            pool.construct(obj, static_cast<int>(i));
            objects.push_back(obj);
        }

        for (TestObject* obj : objects)
        {
            pool.deallocate(obj);
        }

        LOG_INFO("使用内存池 数量: %d 耗时:%.3lfms", count, timer.Elapsed());
    };

    auto NewTest = [](int count)
    {
        Timer timer;
        std::vector<TestObject*> objects;
        objects.reserve(count);
        for (size_t i = 0; i < count; ++i)
        {
            objects.push_back(new TestObject(i));
        }

        for (TestObject* obj : objects)
        {
            delete obj;
        }

        LOG_INFO("不使用内存池 数量: %d 耗时:%.3lfms", count, timer.Elapsed());
    };

    MemoryPoolTest(2000);
    NewTest(2000);

    MemoryPoolTest(200000);
    NewTest(200000);

    MemoryPoolTest(2000000);
    NewTest(2000000);
}
